package com.zjuee.web.basic.dict.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjuee.base.model.common.basic.Dict;
import com.zjuee.web.basic.dict.mapper.DictMapper;
import com.zjuee.service.basic.dict.DictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author wu liang
 * @since 1.0
 */
@Service
@Transactional(readOnly = true)
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {


    @Resource
    private DictMapper dictMapper;


    @Override
    public List<Dict> findByParentId(Long parentId) {
        LambdaQueryWrapper<Dict> queryWrapper = Wrappers.<Dict>query().lambda();
        Optional.ofNullable(parentId).ifPresent(x -> {queryWrapper.eq(Dict::getParentId, x);
                        queryWrapper.orderByAsc(Dict::getSort);});
        return dictMapper.selectList(queryWrapper);
    }

    @Override
    public List<Dict> findAll() {
        LambdaQueryWrapper<Dict> queryWrapper = Wrappers.<Dict>query().lambda();
        return dictMapper.selectList(queryWrapper);
    }

    @Override
    public List<Dict> findListByLevelCode(String levelCode) {
        LambdaQueryWrapper<Dict> queryWrapper = Wrappers.<Dict>query().lambda();
        Optional.ofNullable(levelCode).ifPresent(x -> queryWrapper.like(Dict::getLevelCode, x));
        return dictMapper.selectList(queryWrapper);
    }

}
